package dbs

import (
	"github.com/jinzhu/gorm"
	"time"
)

type DbPlatformToken struct {
	ID        uint      `json:"id" gorm:"primary_key"`
	Token     string    `json:"token" gorm:"type:varchar(255);not null;unique"`      //令牌
	Remark    string    `json:"remark" gorm:"type:varchar(255);not null;default:''"` //说明
	Ip        string    `json:"ip" gorm:"type:varchar(32)"`                          // ip
	EndAt     time.Time `json:"end_at" gorm:"type:datetime"`                         // 到期时间
	CreatedBy string    `json:"created_by" gorm:"type:varchar(64)"`                  // 创建人
	UpdateBy  string    `json:"update_by" gorm:"type:varchar(64)"`                   // 最近更改的人
	CreatedAt time.Time `json:"created_at" gorm:"type:datetime"`
	UpdatedAt time.Time `json:"updated_at" gorm:"type:datetime"`
}

type TokenCreate struct {
	Remark string `json:"remark"`
	Ip     string `json:"ip" validate:"required" label:"ip"`
	EndAt  string `json:"end_at" validate:"required" label:"过期时间"`
}

type TokenList struct {
	EndAt   string `json:"end_at"`
	StartAt string `json:"start_at"`
	Ip      string `json:"ip"`
	Token   string `json:"token"`
	Page    int    `json:"page" label:"页数"`
	Limit   int    `json:"limit" label:"每页显示数"`
}

func (c *DbPlatformToken) Create() (err error) {
	err = D_Pool(func(db **gorm.DB) (err error) {
		conn := *db
		return conn.Create(&c).Error
	})
	return
}

func (c *DbPlatformToken) Get() (err error) {
	err = D_Pool(func(db **gorm.DB) (err error) {
		conn := *db
		if c.Token != "" {
			conn = conn.Where("`token` = ?", c.Token)
		}
		return conn.First(&c).Error
	})
	return
}

func (c *DbPlatformToken) GetList(req TokenList) (data []DbPlatformToken, err error) {
	err = D_Pool(func(db **gorm.DB) (err error) {
		conn := *db
		conn = c.listQuery(conn, req)
		// 分页
		if req.Page != 0 && req.Limit != 0 {
			conn = conn.Offset((req.Page - 1) * req.Limit).Limit(req.Limit)
		}

		return conn.Model(&c).Order("id DESC").Find(&data).Error
	})
	return
}

func (c *DbPlatformToken) CountList(req TokenList) (number int, err error) {
	err = D_Pool(func(db **gorm.DB) (err error) {
		conn := *db
		conn = c.listQuery(conn, req)

		conn = conn.Model(&c).Count(&number)
		err = conn.Error
		return
	})

	return

}

func (c *DbPlatformToken) listQuery(conn *gorm.DB, req TokenList) *gorm.DB {

	if req.Ip != "" {
		conn = conn.Where("ip = ?", req.Ip)
	}

	if req.StartAt != "" {
		conn = conn.Where("end_at <= ?", req.StartAt)
	}
	if req.EndAt != "" {
		conn = conn.Where("end_at >= ?", req.EndAt)
	}

	if req.Token != "" {
		conn = conn.Where("token like ?", "%"+req.Token+"%")
	}
	return conn
}

func (c *DbPlatformToken) Format() (result map[string]interface{}) {
	//todo last time
	// 格式化用户返回值
	result = map[string]interface{}{
		"id":         c.ID,
		"token":      c.Token,
		"remark":     c.Remark,
		"ip":         c.Ip,
		"end_at":     c.EndAt.Format("2006-01-02 15:04:05"),
		"created_by": c.CreatedBy,
		"update_by":  c.UpdateBy,
		"created_at": c.CreatedAt.Format("2006-01-02 15:04:05"),
		"update_at":  c.UpdatedAt.Format("2006-01-02 15:04:05"),
	}
	return
}
